import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/ces',
        name: 'Ces',
        component: () => import('@/views/Cesium.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Main.vue')
    },
    {
        path: '/about',
        name: 'About',
        component: () => import('@/views/About.vue')
    },
    {
        path: '/articleEditor',
        name: 'ArticleEditor',
        component: () => import('@/views/ArticleEditor.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue')
    },
    { path: '/', redirect: { name: 'Home' } }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/' || to.path === '/login') {
        next();
    } else {
        //取出localStorage判断
        let token = localStorage.getItem('token ');
        if (token == null || token === '') {
            console.log('请先登录')
            next({ name: 'Login' });
        } else {
            next();
        }
    }
});
export default router
